package com.example.marketapp;

import com.example.marketapp.dao.ClienteDAO;
import com.example.marketapp.model.Cliente;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;


public class CadastroClienteActivity extends ActionBarActivity {
	
	EditText Nome, CPF, Email, Senha;
	private Cliente cliente;
	ClienteDAO clienteDAO;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_cadastro_cliente);
		
		clienteDAO = new ClienteDAO(this);
		
		
		Button botaoInserir = (Button) findViewById(R.id.botaoInserir);
		Button botaoCancelar = (Button) findViewById(R.id.botaoCancelar);
		final EditText Nome = (EditText) findViewById(R.id.campoNome);
		final EditText CPF = (EditText) findViewById(R.id.campoCPF);
		final EditText Email = (EditText) findViewById(R.id.campoEmail);
		final EditText Senha = (EditText) findViewById(R.id.campoSenha);
		
		if(cliente == null){
			cliente = new Cliente();
		} else{
			botaoCancelar.setText("Cancelar");
		}
		
		botaoInserir.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				cliente.setNome(Nome.getText().toString());
				cliente.setCpf(CPF.getText().toString());
				cliente.setEmail(Email.getText().toString());
				cliente.setSenha(Senha.getText().toString());
				
				if(cliente.getId() == null){
					clienteDAO.insert(cliente);
					
				} 
				finish();				
			}
		});
		
		botaoCancelar.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(CadastroClienteActivity.this, LoginActivity.class);
				startActivity(intent);				
			}
		});
	
		
	
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.login, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

}
